package com.basics.framework.web.domain;


import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class RestResult<T> implements Serializable {
    private static final long serialVersionUID = -802330797309375384L;
    @ApiModelProperty("返回对象")
    private T data;
    @ApiModelProperty("错误码(500:失败 408:登录失效 300:警告提示 200:成功)")
    private Integer code;
    @ApiModelProperty("提示语")
    private String msg;

    public RestResult() {
    }

    public RestResult(String msg, Integer code, T data) {
        this.msg = msg;
        this.code = code;
        this.data = data;
    }

    public static <T> RestResult<T> error() {
        return error(500, "操作失败");
    }

    public static <T> RestResult<T> loginError() {
        return error(408, "登录失效");
    }

    public static <T> RestResult<T> error(String msg) {
        return error(500, msg);
    }

    public static <T> RestResult<T> warn(String msg) {
        return error(300, msg);
    }

    public static <T> RestResult<T> error(int code, String msg) {
        return new RestResult(msg, code, null);
    }

    public static <T> RestResult<T> success(String msg) {
        return new RestResult(msg, 200, new JSONObject());
    }

    public static <T> RestResult<T> success() {
        return success("操作成功");
    }

    public static <T> RestResult<T> success(T data) {
        RestResult<T> json = success();
        if (null != data) {
            json.setData(data);
        }

        return json;
    }

    public RestResult<T> onBindingError(List<ObjectError> errors) {
        return new RestResult(praseErrorMessage(errors), 500, new JSONObject());
    }

    public static String praseErrorMessage(List<ObjectError> errors) {
        StringBuffer stringBuffer = new StringBuffer();
        Iterator var2 = errors.iterator();

        while(var2.hasNext()) {
            ObjectError error = (ObjectError)var2.next();
            if (error instanceof FieldError) {
                FieldError fieldError = (FieldError)error;
                stringBuffer.append(fieldError.getDefaultMessage());
                stringBuffer.append(",");
            }
        }

        return stringBuffer.toString().substring(0, stringBuffer.toString().length() - 1);
    }

    public T getData() {
        return this.data;
    }

    public Integer getCode() {
        return this.code;
    }

    public String getMsg() {
        return this.msg;
    }

    public void setData(final T data) {
        this.data = data;
    }

    public void setCode(final Integer code) {
        this.code = code;
    }

    public void setMsg(final String msg) {
        this.msg = msg;
    }

}

